package com.example.taobaounion.ui.adapter;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.taobaounion.R;
import com.example.taobaounion.model.bean.IBaseInfo;
import com.example.taobaounion.model.bean.SelectContent;
import com.example.taobaounion.utils.Constants;
import com.example.taobaounion.utils.LogUtils;
import com.example.taobaounion.utils.UrlUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class SelectedPageContentAdapter extends RecyclerView.Adapter<SelectedPageContentAdapter.InnerHolder> {

    private List<SelectContent.DataBean.TbkDgOptimusMaterialResponseBean.ResultListBean.MapDataBean> mData=
            new ArrayList<>();
    private OnSelectedPageContentItemClickListener mContentItemClickListener=null;

    @Override
    public SelectedPageContentAdapter.InnerHolder onCreateViewHolder(@NonNull  ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_selected_page_content, parent, false);

        return new InnerHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull  SelectedPageContentAdapter.InnerHolder holder, int position) {
        SelectContent.DataBean.TbkDgOptimusMaterialResponseBean.ResultListBean.MapDataBean itemData = mData.get(position);
        holder.setData(itemData);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mContentItemClickListener.onContentItemClick(itemData);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    public void setData(SelectContent content) {
        if(content.getCode()== Constants.SUCCESS_CODE){
            List<SelectContent.DataBean.TbkDgOptimusMaterialResponseBean.ResultListBean.MapDataBean>
                    data_tbk_item = content.getData().getTbk_dg_optimus_material_response()
                    .getResult_list().getMap_data();
            mData.clear();
            mData.addAll(data_tbk_item);
            notifyDataSetChanged();
        }
    }

    public class InnerHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.selected_cover)
        public ImageView cover;
        @BindView(R.id.selected_off_prise)
        public TextView offPriseTv;
        @BindView(R.id.selected_title)
        public TextView title;
        @BindView(R.id.selected_buy_btn)
        public TextView buyBtn;
        @BindView(R.id.selected_original_prise)
        public TextView originalPriseTv;

        public InnerHolder(@NonNull  View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }

        public void setData(SelectContent.DataBean.TbkDgOptimusMaterialResponseBean.ResultListBean.MapDataBean itemData) {
            LogUtils.d(this,"itemData.getTitle()-->"+itemData.getTitle());
            title.setText(itemData.getTitle());
            String coverPath = UrlUtils.getCoverPath(itemData.getPict_url());
            LogUtils.d(this,"itemData.getPict_url()--->"+itemData.getPict_url());
            //这里的itemView就是上面的new InnerHolder(itemView)
            Glide.with(itemView.getContext()).load(coverPath).into(cover);
            if(TextUtils.isEmpty(itemData.getCoupon_click_url())){
                originalPriseTv.setText("抱歉，没有优惠劵了");
                buyBtn.setVisibility(View.GONE);
            }else {
                originalPriseTv.setText("原价：" + itemData.getZk_final_price());
                buyBtn.setVisibility(View.GONE);
            }
            if(TextUtils.isEmpty(itemData.getCoupon_info())){
                offPriseTv.setVisibility(View.GONE);
            }else {
                offPriseTv.setVisibility(View.VISIBLE);
                offPriseTv.setText(itemData.getCoupon_info());
            }

        }
    }

    public void setOnSelectedPageContentItemClickListener(OnSelectedPageContentItemClickListener listener){
        this.mContentItemClickListener=listener;
    }
    public interface OnSelectedPageContentItemClickListener{
        void onContentItemClick(IBaseInfo item);
    }
}
